package org.jeecg.modules.utils;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import org.jeecg.modules.gqHealth.entity.GqCardManager;

import java.time.LocalDate;
import java.util.*;

public class CardGenerator {
    // 生成卡号的规则
    private static final String CARD_PREFIX = LocalDate.now().getYear()+"";
    private static final int CARD_NUMBER_LENGTH = 12;

    // 生成卡号
    public static String generateCardNumber() {
        String cardNumber = CARD_PREFIX;
        Random random = new Random();
        for (int i = 0; i < CARD_NUMBER_LENGTH - CARD_PREFIX.length(); i++) {
            cardNumber += random.nextInt(10);
        }
        return cardNumber;
    }

    // 生成防伪码
    public static String generateAntiCollisionCode(int length) {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < length; i++) {
            char c = (char) (random.nextInt(26) + 'A'); // 生成随机大写字母
            code += c;
        }
        return code;
    }

    /**
     * 生成卡号
     * @param TOTAL_CARDS
     * @return
     */
    public static List<GqCardManager> generateCard(int TOTAL_CARDS){
        List<GqCardManager> cardNumbers = new ArrayList<>();
        while (cardNumbers.size() < TOTAL_CARDS) {
            GqCardManager bean = new GqCardManager();
            bean.setCardNo(generateCardNumber());
            bean.setSecurityCode(generateAntiCollisionCode(4));
            bean.setId(new DefaultIdentifierGenerator().nextId(new GqCardManager()).toString());
            cardNumbers.add(bean);


        }
        return cardNumbers;
    }

    public static void main(String[] args) {
        String cardNumber = generateCardNumber();
        String antiCollisionCode = generateAntiCollisionCode(4); // 生成4位防伪码
        System.out.println("卡号: " + cardNumber + " 防伪码: " + antiCollisionCode);
        UUID uid = UUID.randomUUID();
        System.out.println(uid.toString());
    }
}
